package com.yunkeji.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.exception.ServiceException;
import com.yunkeji.dao.domain.AppUser;
import com.yunkeji.dao.domain.AppUserSpecial;
import com.yunkeji.dao.mapper.AppUserMapper;
import com.yunkeji.dao.mapper.AppUserSpecialMapper;
import com.yunkeji.dao.vo.AppUserSpecialVo;
import com.yunkeji.system.domain.bo.AppUserSpecialBo;
import com.yunkeji.system.service.IAppUserSpecialService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户钱包信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class AppUserSpecialServiceImpl implements IAppUserSpecialService {

    private final AppUserSpecialMapper baseMapper;
    private final AppUserMapper userMapper;

    /** 查询用户钱包信息列表 */
    @Override
    public TableDataInfo<AppUserSpecialVo> queryPageList(AppUserSpecialBo bo, PageQuery pageQuery) {
        Page<AppUserSpecialVo> result = baseMapper.selectListPage(bo.getPlatformId(), pageQuery.build());
        return TableDataInfo.build(result);
    }

    /** 新增用户钱包信息 */
    @Override
    public Boolean insertByBo(AppUserSpecialBo bo) {
        Long count = baseMapper.selectCount(Wrappers.lambdaQuery());
        if(count + 1 > 50){
            throw new ServiceException("特殊权限用户只能设置50个！");
        }
        AppUser user = userMapper.selectOne(Wrappers.lambdaQuery(AppUser.class).eq(AppUser::getPlatformId, bo.getPlatformId()));
        if(Objects.isNull(user)){
            throw new ServiceException("未找到用户，请正确填写爱洞ID！");
        }
        AppUserSpecial add = new AppUserSpecial();
        add.setId(user.getId());
        add.setRemark(bo.getRemark());
        return baseMapper.insert(add) > 0;
    }

    /** 批量删除用户钱包信息 */
    @Override
    public Boolean deleteWithValidByIds(Long id) {
        return baseMapper.deleteById(id) > 0;
    }
}
